package com.leyu.mall.admin.goods.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.leyu.mall.domain.goods.GoodsAlbum;
import com.leyu.mall.domain.goods.GoodsBase;
import com.leyu.mall.domain.goods.GoodsCategory;
import com.leyu.mall.domain.goods.GoodsMobile;
import com.leyu.mall.domain.goods.GoodsRelation;
import com.leyu.mall.domain.goods.GoodsValueadd;
import com.leyu.mall.domain.mdm.MDMColor;
import com.leyu.mall.domain.mdm.MDMFeature;
import com.leyu.mall.domain.mdm.MDMOs;
import com.leyu.mall.domain.mdm.MDMPriceRegion;
import com.leyu.mall.domain.mdm.MDMProvince;
import com.leyu.mall.domain.mdm.MDMScreensize;
import com.leyu.mall.domain.mdm.MDMStorage;
import com.leyu.mall.service.cms.ICmsChannelService;
import com.leyu.mall.service.goods.IGoodsAlbumService;
import com.leyu.mall.service.goods.IGoodsBaseService;
import com.leyu.mall.service.goods.IGoodsBrandService;
import com.leyu.mall.service.goods.IGoodsCategoryService;
import com.leyu.mall.service.goods.IGoodsMobileService;
import com.leyu.mall.service.goods.IGoodsRelationService;
import com.leyu.mall.service.mdm.IMDMColorService;
import com.leyu.mall.service.mdm.IMDMFeatureService;
import com.leyu.mall.service.mdm.IMDMOSService;
import com.leyu.mall.service.mdm.IMDMPriceRegionService;
import com.leyu.mall.service.mdm.IMDMProvinceService;
import com.leyu.mall.service.mdm.IMDMScreenSizeService;
import com.leyu.mall.service.mdm.IMDMStorageService;
/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/goods")
public class GoodsBaseController
{
	@Resource
	private IGoodsBaseService		goodsBaseServiceImpl;
	@Autowired
	private IGoodsBrandService		goodsBrandServiceImpl;
	@Autowired
	private IGoodsCategoryService	goodsCategoryService;
	@Autowired
	private ICmsChannelService		cmsChannelService;

	@Autowired
	private IGoodsRelationService goodsRelationServiceImpl;
	@Autowired
	IGoodsAlbumService goodsAlbumServiceImpl;
	
	@Autowired
	IGoodsMobileService goodsMobileServiceImpl;
	
	
	
	@Autowired
	IMDMProvinceService  mdmProvinceServiceImpl;
	@Autowired
	IMDMPriceRegionService  mdmPriceRegionServiceImpl;
	
	@Autowired
	IMDMOSService  mdmOsServiceImpl;
	@Autowired
	IMDMColorService  mdmColorServiceImpl;
	@Autowired
	IMDMScreenSizeService  mdmScreensizeServiceImpl;
	
	
	@Autowired
	IMDMStorageService  mdmStorageServiceImpl;
	@Autowired
	IMDMFeatureService  mdmFeatureServiceImpl;
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, GoodsBase goodsBase,GoodsAlbum goodsAlbum,GoodsMobile goodsMobile,GoodsValueadd goodsValueadd )
	{
		try
		{
			
			System.out.println("------goodsAlbum------"+goodsAlbum.getImgUrl());
			
			System.out.println("GoodsBaseController:save2"+goodsBase.getRelationId());
			String relationids [] = goodsBase.getRelationId().split(",");
			System.out.println("GoodsBaseController:relationids"+relationids);
			int goodsidd =  goodsBaseServiceImpl.save(goodsBase);
			System.out.println("---------------------goodsBaseServiceImp------------------"+goodsBase.getGoodsId());
//			System.out.println("---------------------goodsBaseServiceImp------------------"+goodsidd);
			//保存推荐关联商品
			List<GoodsRelation> grlist = new ArrayList();
			for(int i = 0 ; i < relationids.length; i ++){
				GoodsRelation goodsRelation = new GoodsRelation();
				goodsRelation.setGoodsId(goodsBase.getGoodsId());
				goodsRelation.setReGoodsId(Long.parseLong(relationids[i]));
				goodsRelation.setGoodsType(goodsBase.getGoodsType());
				grlist.add(goodsRelation);
			}
			goodsRelationServiceImpl.save(grlist);
			//保存商品相册
			goodsAlbum.setGoodsId(goodsBase.getGoodsId());
			goodsAlbumServiceImpl.save(goodsAlbum);
			goodsMobile.setGoodsId(goodsBase.getGoodsId());
			this.goodsMobileServiceImpl.save(goodsMobile);
			
			
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../goods";
	}

	@RequestMapping(value = "delete/{goodsID}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long goodsID)
	{
		try
		{
			goodsBaseServiceImpl.deleteGoodsBase(goodsID);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../goods";
	}

	@RequestMapping(value = "edit/{goodsID}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long goodsID)
	{
		try
		{
			model.addAttribute("entity", goodsBaseServiceImpl.getGoodsBaseById(goodsID));
			model.addAttribute("brandlist", goodsBrandServiceImpl.getAllDisplayGoodsBrand());
			model.addAttribute("categorylist", goodsCategoryService.getAllDisplayCategoryList());
			model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editgoods.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request)
	{
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newBrand(Model model,@RequestParam(value = "curPage", required = false, defaultValue ="1" ) int curPage) throws Exception
	{
		System.out.println("---------------");
		model.addAttribute("brandlist", goodsBrandServiceImpl.getAllDisplayGoodsBrand());
		List<GoodsCategory> categorylist = goodsCategoryService.getAllDisplayCategoryList();
		model.addAttribute("categorylist", categorylist);
		model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		
		List<GoodsBase> goodslist = goodsBaseServiceImpl.getAllGoodsBase();
		model.addAttribute("goodslist", goodslist);
//		 
	
		
//		List twodepthList = new ArrayList();
		Map datamap = new HashMap();
//		for(GoodsCategory cate : categorylist){
			 
			for(GoodsBase goods : goodslist ){
				if(!datamap.containsKey(goods.getCategoryId())){
					List datalist = new ArrayList();
					datalist.add(goods);
					datamap.put(goods.getCategoryId(), datalist);
				}else  {
					
					List oldlist = (List)datamap.get(goods.getCategoryId());
					oldlist.add(goods);
					datamap.put(goods.getCategoryId(), oldlist);
				}
		}
		
		model.addAttribute("datamap", datamap);
		
		GoodsBase pagebean = new GoodsBase();
		int totalItem = goodsBaseServiceImpl.getGoodsListByPageCount();
		pagebean.setTotalItem(totalItem);
		pagebean.setCurPage(curPage);
		model.addAttribute("goodslist", goodsBaseServiceImpl.getGoodsListByPage(pagebean));
//		List<GoodsBase> goodslist = goodsBaseServiceImpl.getGoodsListByPage(pagebean);
		model.addAttribute("pagebean", pagebean);
		List<MDMProvince> provinceList = this.mdmProvinceServiceImpl.getAllMDMProvince();
		List<MDMPriceRegion> priceRegionList = this.mdmPriceRegionServiceImpl.getMDMPriceRegionList();
		List<MDMOs> osList = this.mdmOsServiceImpl.getMDMOSList();
		List<MDMColor> colorList = this.mdmColorServiceImpl.getMDMColorList();
		List<MDMScreensize> screensizeList = this.mdmScreensizeServiceImpl.getMDMScreenSizeList();
		List<MDMStorage> storageList = this.mdmStorageServiceImpl.getMDMStorageList();
		List<MDMFeature> featureList = this.mdmFeatureServiceImpl.getMDMFeatureList();
		
		model.addAttribute("storageList", storageList);
		model.addAttribute("featureList", featureList);
		model.addAttribute("osList", osList);
		model.addAttribute("colorList", colorList);
		model.addAttribute("screensizeList", screensizeList);
		model.addAttribute("provinceList", provinceList);
		model.addAttribute("priceRegionList", priceRegionList);
		return "admin/newgoods.html";
	}
	
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model,@RequestParam(value = "curPage", required = false, defaultValue ="1" ) int curPage) throws Exception
	{
		try
		{
			GoodsBase pagebean = new GoodsBase();
			int totalItem = goodsBaseServiceImpl.getGoodsListByPageCount();
			pagebean.setTotalItem(totalItem);
			pagebean.setCurPage(curPage);
			model.addAttribute("goodslist", goodsBaseServiceImpl.getGoodsListByPage(pagebean));
			model.addAttribute("pagebean", pagebean);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/goods.html";
	}
}
